package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"learning/gooop/saga/mqs/logger"
	"learning/gooop/saga/mqs/models"
	"net/http"
)

func Notify(c *gin.Context) {
	body := c.Request.Body
	defer body.Close()

	j, e := ioutil.ReadAll(body)
	if e != nil {
		logger.Logf("handlers.Notify, failed ioutil.ReadAll")
		c.JSON(http.StatusBadRequest, gin.H { "ok": false, "error": e.Error()})
		return
	}

	msg := &models.TxMsg{}
	e = json.Unmarshal(j, msg)
	if e != nil {
		logger.Logf("handlers.Notify, failed json.Unmarshal")
		c.JSON(http.StatusBadRequest, gin.H { "ok": false, "error": e.Error()})
		return
	}

	logger.Logf("handlers.Notify, msg=%v", msg)
	c.JSON(http.StatusOK, gin.H { "ok": true })
}
